﻿// FLMScanFactory
// Factory for creation of FLMScan instances
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.Text;
using log4net;
using System.IO;
using FLMScan.Rotator;
using FLMScan.WorldCoordinateReconstruction.Riedmaier;
using FLMScan.Parameters;
using FLMScan.Exception;
using FLMScan.Capture;

namespace FLMScan
{
    class FLMScanFactory
    {
        public static FLMScanInstance Make(FLMScanInstanceType instanceType, IFLMScanView view, FLMScanViewModel viewModel, FLMScanParameters parameters)
        {
            FLMScanInstance flmScan = null;

            switch(instanceType)
            {
                case FLMScanInstanceType.EProductional:                    

                    RiedmaierReconstruction riedmaierReconstruction = 
                        RiedmaierReconstructionFactory.Make(parameters.CameraSensorParameters, parameters.LaserPlaneParameters, parameters.RotationAxisParameters);
                    
                    ICapture capture = null;
                    IStepRotator stepRotator = null;
                    IFreeRotator freeRotator = null;

                    ILog logger = LogManager.GetLogger(typeof(FLMScanFactory));
                    logger.Info("Starting up FLMScan...");

                    try
                    {                        
                        capture = new WebcamCapture(parameters.CameraParameters, parameters.CameraSensorParameters);                        
                        logger.Info("WebcamCapture constructed");                        
                    }
                    catch (CaptureException ex)
                    {
                        logger.Error("WebcamCapture initialization failed: " + ex);
                    }

                    try
                    {
                        // TBD: retrieve this from configuration
                        int MaxStepCount = 200;

                        // IRotator rotator = new ManualRotator(ManualRotator.BeepType.ESimpleBeep, 3000);
                        stepRotator = new ArduinoStepperRotator(parameters.ArduinoSerialPort, MaxStepCount);
                        freeRotator = new ArduinoFreeRotator(parameters.ArduinoSerialPort, MaxStepCount);
                        logger.Info("Arduino stepper rotator constructed");
                    }
                    catch (SerialPortException ex)
                    {
                        logger.Error("Serial port initialization failed: " + ex);
                    }
                    
                    System.Diagnostics.Debug.Assert(capture != null);
                    System.Diagnostics.Debug.Assert(stepRotator != null);
                    System.Diagnostics.Debug.Assert(freeRotator != null);
                    flmScan = FLMScanInstance.Instance(view, capture, stepRotator, freeRotator, parameters, riedmaierReconstruction, viewModel);

                    logger.Info("FLMScan instance constructed");                                        
                    break;

                case FLMScanInstanceType.ETest:
                    // TBD: implement this
                    break;
            }            

            return flmScan;
        }        
    }    
}
